/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include "hotspotmap.h"

HotspotMap::HotspotMap(IrrlichtDevice *device, const c8 *map, ENODE_TYPE type,
    SColor spotColor)
  {
    Device = device;
    Map = map;
    Hotspot = spotColor;
    Type = type;
    if (type == ENT_CUBIC || ENT_SPHERICAL)
    //Pointer = Device->getSceneManager()->addCameraSceneNodeFPS(0, 100.0f, 0);
      Pointer = Device->getSceneManager()->getActiveCamera();//->clone();
  }

HotspotMap::~HotspotMap()
  {
    texture->drop();
    image->drop();
  }

SColor HotspotMap::getHotspot()
  {
    ICursorControl *cursorCtrl = Device->getCursorControl();
    ITexture *texture = Device->getVideoDriver()->getTexture(Map);
    IImage* image = Device->getVideoDriver()->createImageFromFile(Map);

    if (Type == ENT_CUBIC || ENT_SPHERICAL)
      {
        u32 az, el;
        az = (u32)Pointer->getTarget().Y;
        el = (u32)Pointer->getTarget().X;
        az = az * 360 / (u32)texture->getSize().Width;
        el = (u32)sin((long double)el)/2 * texture->getSize().Height; //a.viarengo added cast

        image->lock();
        return image->getPixel(az,el);
        image->unlock();
      }
    else
      {
        s32 x,y;
        x = cursorCtrl->getPosition().X;
        y = cursorCtrl->getPosition().Y;
        x = x * Device->getVideoDriver()->getScreenSize().Width / texture->getSize().Width;
        y = y * Device->getVideoDriver()->getScreenSize().Height / texture->getSize().Height;
        image->lock();
        return image->getPixel(x,y);
        image->unlock();
      }
  }
